package com.hxkcloud.patient.aerotel.provider;

import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

import com.hxkcloud.patient.aerotel.database.DatabaseHelper;
import com.hxkcloud.patient.aerotel.database.ECGTable;


public class ECGContentProvider extends HDContentProvider {

    static {
        urimatcher.addURI(AUTHORITY, "ecgs", QUERY_LIST);
        urimatcher.addURI(AUTHORITY, "ecgs/#", QUERY_ONE);
    }

    private SQLiteDatabase db;
    private DatabaseHelper dbhelper;

    @Override
    public boolean onCreate() {
        dbhelper = new DatabaseHelper(getContext());
        return dbhelper != null ? true : false;

    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        switch (urimatcher.match(uri)) {
            case QUERY_LIST:
                break;
            case QUERY_ONE:
                selection = ECGTable._ID + "=" + uri.getPathSegments().get(1);
                break;
            default:
                throw new IllegalArgumentException("Unknow URI: " + uri);
        }
        db = dbhelper.getWritableDatabase();
        if (db != null) {
            return db.delete(ECGTable.TABLE_NAME, selection, selectionArgs);
        }
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        switch (urimatcher.match(uri)) {
            case QUERY_LIST:
                return ECGTable.COLLECTION_CONTENT_TYPE;
            case QUERY_ONE:
                return ECGTable.CONTENT_TYPE;
            default:
                throw new IllegalArgumentException("Unknow URI: " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        db = dbhelper.getWritableDatabase();
        if (db != null) {
            long rowid = db.insert(ECGTable.TABLE_NAME, "", values);
            if (rowid > 0) {
                Uri rowUri = ContentUris.appendId(ECGTable.CONTENT_URI.buildUpon(), rowid).build();
                getContext().getContentResolver().notifyChange(rowUri, null);
                return rowUri;
            }
        }
        return null;
    }


    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(ECGTable.TABLE_NAME);
        queryBuilder.setProjectionMap(ECGTable.PROJECTION_MAP);
        switch (urimatcher.match(uri)) {
            case QUERY_LIST:
                break;
            case QUERY_ONE:
                queryBuilder.appendWhere(ECGTable._ID + "=" + uri.getPathSegments().get(1));
                break;
            default:
                throw new IllegalArgumentException("Unknow URI: " + uri);
        }

        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = ECGTable.DEFAULT_ORDERBY;
        } else {
            orderBy = sortOrder;
        }

        db = dbhelper.getReadableDatabase();
        if (db != null) {
            Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, "", "", sortOrder);
            cursor.setNotificationUri(getContext().getContentResolver(), uri);
            return cursor;
        }
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        switch (urimatcher.match(uri)) {
            case QUERY_LIST:
                break;
            case QUERY_ONE:
                selection = ECGTable._ID + "=" + uri.getPathSegments().get(1);
                break;
            default:
                throw new IllegalArgumentException("Unknow URI: " + uri);
        }
        db = dbhelper.getWritableDatabase();
        if (db != null) {
            int r = db.update(ECGTable.TABLE_NAME, values, selection, selectionArgs);
            if (r > 0) {
                getContext().getContentResolver().notifyChange(ECGTable.CONTENT_URI, null);
            }
            return r;
        }
        return 0;
    }
}
